import pygame
from MainWindow import MainWindow
from Person import Person
from S_Wall import S_Wall
from D_Wall import D_Wall
from Door import Door
from Door_Person import Door_Person
from Spike import Spike


class MainGame_five(MainWindow):

    def __init__(self):
        super().__init__()

        self.person = Person(70, 700)  # 人物初始化
        self.S_Walls.extend([
            S_Wall(0, 700, 1000, 100),
            S_Wall(960, 650, 40, 50),
            S_Wall(0, 600, 900, 40),
            S_Wall(0, 550, 40, 50),
            S_Wall(100, 500, 900, 40),
            S_Wall(960, 450, 40, 50),
            S_Wall(0, 400, 900, 40),
            S_Wall(0, 350, 40, 50),
            S_Wall(100, 300, 800, 40),
            S_Wall(950, 300, 50, 40),
            ])  # 静态墙初始化 [x,y]
        self.D_Walls.extend([
            D_Wall(900, 300, 50, 40, 'x', 6,True,70)
        ])
        self.One_spike.extend([
            Spike(200,0,'y',6),
            Spike(250, 300, 'y',7),
            Spike(300, 0, 'y',8),
            Spike(350, 100, 'y',7),
            Spike(400, 600, 'y',8),
            Spike(450, 200, 'y',6),
            Spike(500, 0, 'y', 7),
            Spike(550, 100, 'y', 8),
            Spike(600, 500, 'y', 6),
            Spike(650, 250, 'y', 7),
            Spike(700, 150, 'y', 8),
            Spike(750, 250, 'y', 7),

        ])
        self.all_sprites.add(self.person)
        for wall in self.S_Walls:
            self.obstacles.add(wall)
        for wall in self.D_Walls:
            self.D_obstacles.add(wall)

        self.door = Door(900, 300)  # 出口初始化
        self.door_person = Door_Person(self.door.rect.x, self.door.rect.y)  # 出口动画初始化

    def StartGame(self):
        super().Start()
        while True:
            dt = self.clock.tick(60) / 1000  # 获取delta time用于平滑移动
            self.GetEvent()
            self.update(dt)
            self.render()
            self.brith()
            pygame.display.flip()
            #后续添加判断人门图片是否存在，然后结束本关卡。
            if not self.door_person.alive:
                break
    # 渲染窗口
    def render(self):
        super().render()


                    
        if not self.person.rect.colliderect(self.door.rect):
            self.all_sprites.draw(self.window)
            self.door.Door_display(self.window)
            self.door.door_move(self.person, 'x', 6, 50, 67)
            self.door_person.__init__(self.door.rect.x, self.door.rect.y)
        else:
            self.reached_door = True
            self.door_person.img_move('y',3)
            self.door_person.move_img_display(self.window)
        for d_wall in self.D_obstacles:
            if d_wall.alive == True:
                d_wall.move_img_display(self.window)
            else:
                if d_wall.visible == False:
                    self.window.blit(d_wall.image,d_wall.rect)
                    d_wall.wall_move()